#include	"Timer.h"
#include	<windows.h>

Timer::Timer()
	:	m_threadId(0)
	,	m_processAffinityMask(0)
	,	m_frequency(0)
	,	m_lastTime(0)
{
	m_threadId = ::GetCurrentThread();
	DWORD_PTR system_mask;
	::GetProcessAffinityMask(::GetCurrentProcess(), &m_processAffinityMask, &system_mask);
	::SetThreadAffinityMask(m_threadId, 1);
	::QueryPerformanceFrequency(reinterpret_cast< LARGE_INTEGER * >(&m_frequency));
	::SetThreadAffinityMask(m_threadId, m_processAffinityMask);
}

Timer::~Timer()
{
}

void Timer::Start()
{
	    m_lastTime = GetTime();
}

double Timer::Time()
{
	__int64 current = GetTime();
	__int64 delta = current - m_lastTime;
	m_lastTime = current;
	return delta / static_cast< double >(m_frequency);
}

__int64 Timer::GetTime() const
{
	LARGE_INTEGER current_time;
	::SetThreadAffinityMask(m_threadId, 1);
	::QueryPerformanceCounter(&current_time);
	::SetThreadAffinityMask(m_threadId, m_processAffinityMask);
	return current_time.QuadPart;
}
